package com.mapforge.gui;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Iterator;

import javax.swing.JLabel;

import com.mapforge.gui.interfaces.MapToolGUIInterface;
import com.mapforge.gui.interfaces.StatusLabelGUIInterface;
import com.mapforge.listener.MapToolChangeEvent;
import com.mapforge.listener.TileLayerDeletionEvent;
import com.mapforge.listener.TileLayerEditEvent;
import com.mapforge.listener.TileLayerSelectionEvent;
import com.mapforge.listener.TileMapCreationEvent;
import com.mapforge.map.TileLayer;
import com.mapforge.map.TileMap;

/** */
public class StatusLabel extends JLabel implements StatusLabelGUIInterface {
	/**
	 * Default serial version ID, here to remove warnings
	 */
	private static final long serialVersionUID = 1L;

	/** */
	private String mapName;

	/** */
	private Point mapTilePos;

	/** */
	private String selectedTool;

	private TileMap tileMap;

	/** */
	private String selectedLayerName;

	public StatusLabel() {
		this.mapName = "";
		this.mapTilePos = new Point();
		this.selectedTool = "No selected tool";
		this.selectedLayerName = "No layer selected";
	}

	private void updateText() {
		this.setText(selectedTool+", "+mapName + " : " + selectedLayerName);
	}

	/** */
	public String toString() {
		return mapName + " : " + selectedLayerName + " (" + mapTilePos.x + " "
				+ mapTilePos.y + ") " + selectedTool;
	}

	public void tileMapCreationPerformed(TileMapCreationEvent mapCreationEvent) {
		this.tileMap = mapCreationEvent.getTileMapCreated();
		this.mapName = this.tileMap.getName();
		this.updateText();
	}

	public void tileLayerEditPerformed(TileLayerEditEvent layerChangeEvent) {
		TileLayer layer = layerChangeEvent.getTileLayersChanged();
		if (layer != null) {
		} else {
			selectedLayerName = "No layer selected";
			this.updateText();
		}
	}

	@Override
	public void tileLayerDeletionPerformed(TileLayerDeletionEvent event) {
		Iterator<TileLayer> it = event.getDeletedLayerIterator();
		while (it.hasNext()) {
			if (selectedLayerName.equals(it.next().getName())) {
				selectedLayerName = "No layer selected";
				this.updateText();
				break;
			}
		}
	}

	@Override
	public void mapToolChangePerformed(MapToolChangeEvent e) {
		selectedTool = e.getMapTool().getDescription();
		if (e.getSource() instanceof MapToolGUIInterface) {
			MapToolGUIInterface c = (MapToolGUIInterface) e.getSource();
			this.setIcon(c.getIcon());
		} else {
			this.setIcon(null);
		}
		this.updateText();
	}

	@Override
	public void tileLayerSelectionPerformed(TileLayerSelectionEvent event) {
		int num = event.getNumberLayersSelected();
		if (num == 1) {
			Iterator<TileLayer> it = event.iterator();
			this.selectedLayerName = it.next().getName();
		} else {
			this.selectedLayerName = "Multiple Layers";
		}
		this.updateText();
	}

	private void updateMousePosition(MouseEvent e) {
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		this.updateMousePosition(e);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		this.updateMousePosition(e);
	}
}
